package com.cn.medicalcare.controller.backstageservlet.company;

import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.customtool.SqlSessionFactoryTool;
import com.cn.medicalcare.mapper.CompanyMapper;
import com.cn.medicalcare.pojo.AllPage;
import com.cn.medicalcare.pojo.Company;
import com.cn.medicalcare.pojo.Page;
import com.cn.medicalcare.service.CompanyService;
import com.cn.medicalcare.service.serviceimpl.CompanyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/backstage/company/getcompanyinpageby_.json")
public class CompanyInPageServlet extends HttpServlet {

    private CompanyService companyService = CompanyServiceImpl.getInstance();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        String consulttel = req.getParameter("consulttel");
        String advancetel = req.getParameter("advancetel");
        String fax = req.getParameter("fax");
        String email = req.getParameter("email");
        String website = req.getParameter("website");
        String route = req.getParameter("route");
        String microblog = req.getParameter("microblog");
        String wechat = req.getParameter("wechat");
        String survival = req.getParameter("survival");
        String istop = req.getParameter("istop");

        PrintWriter pw = resp.getWriter();
        Map<String, Object> map = new LinkedHashMap<>();
        Company company = new Company();
        try {
            if (survival != null && survival.length() > 0)
                company.setSurvival(Integer.valueOf(survival));
            if (istop != null && istop.length() > 0)
                company.setIstop(Integer.valueOf(istop));
            company.setName(name).setAddress(address).setConsulttel(consulttel)
                    .setAdvancetel(advancetel).setFax(fax).setEmail(email).setWebsite(website).setRoute(route)
                    .setMicroblog(microblog).setWechat(wechat);
            int pagenum = Integer.valueOf(req.getParameter("page"));
            int limit = Integer.valueOf(req.getParameter("limit"));
            Page page = null;
            if (pagenum > 0 && limit > 0)
                page = new Page((pagenum - 1) * limit, limit);
            AllPage<Company> allPage = new AllPage<>(company, page);

            map.put("status", 200);
            map.put("data", companyService.selectCompanyInPage(allPage));
            map.put("count", companyService.selectCompanyCount(company));
        } catch (Exception e) {
            map.put("status", 500);
        }
        pw.print(JsonTool.toJson(map));
    }
}
